package cn.toolck.minitomcat.start;

import cn.toolck.minitomcat.passer.WebXmlPasser;
import cn.toolck.minitomcat.runner.WorkExecutor;
import cn.toolck.minitomcat.runner.WorkRunner;
import cn.toolck.minitomcat.servlet.HttpServlet;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;

/*
 * 需求进行http请求并相应一个动态资源的访问
 * */
public class Starter_0_3 extends AbstractStarter implements Starter {
    private Map<String, HttpServlet> servletMap;
    private static final String WEB_FILE = "web.xml";
    private WorkExecutor executor = new WorkExecutor();
    @Override
    public void init() {
        WebXmlPasser xmlPasser = new WebXmlPasser(WEB_FILE);
        servletMap = xmlPasser.passFile();
    }

    @Override
    public void start() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8083);
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                WorkRunner workRunner = new WorkRunner(socket, servletMap);
                executor.execute(workRunner);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
